<template>
    <div>
        <div class="container" style="margin-left:20px;margin-left:10px">
            <div class="handle-box" style="margin-top:20px">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>
                <el-input v-model="query.name" placeholder="商品名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="data"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="goodsId" label="ID" width="55" align="center">
                </el-table-column>
                <el-table-column prop="goodsName" label="商品名字" align="center" ></el-table-column>
                <el-table-column label="商品价格" align="center">
                    <template #default="scope">￥{{ scope.row.goodsPrice }}</template>
                </el-table-column>
                <el-table-column label="商品图标" align="center">
                    <template #default="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.goodsBigLogo"
                            :preview-src-list="[scope.row.goodsBigLogo]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="goodsIntroduce" label="商品介绍" align="center"></el-table-column>
                <el-table-column label="是否是精品" align="center">
                    <template #default="scope">
                        <el-tag
                            :type="
                                scope.row.hotGoods === '0'
                                    ? 'success'
                                    : scope.row.hotGoods === '1'
                                    ? 'danger'
                                    : ''
                            "
                        >{{ scope.row.hotGoods=='0'?"是":"否" }}</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="createTime" label="上架时间" align="center"></el-table-column>
                
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                    <i class="el-icon-edit" @click="editGoods(scope.row)"></i> 
                    
                    <i class="el-icon-delete" @click="delGoods(scope.row)"></i>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    style="float: right;"
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="goodsData.length"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="商品名称">
                    <el-input v-model="form.goodsName"></el-input>
                </el-form-item>
                <el-form-item label="商品数量">
                    <el-input v-model="form.goodsNumber"></el-input>
                </el-form-item>
                 <el-form-item label="商品金额">
                    <el-input v-model="form.goodsPrice"></el-input>
                </el-form-item>
                <el-form-item label="是否精品">
                    <el-switch
                    v-model="value"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    >
                    </el-switch>
                </el-form-item>
                <el-form-item label="商品图集">
                    <el-input v-model="form.goodsSmallLogo"></el-input>
                </el-form-item>
                 <el-form-item label="商品介绍">
                    <el-input v-model="form.goodsIntroduce"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { goodsAll,getCategory,goodsById,updateGoods,delGoods,seachGoods} from "@/api/index";
export default {
    name: "basetable",
    data() {
        return {
            query: {
                address: "",
                name: "",
                pageIndex: 1, //当前页
                pageSize: 5   //页面所显示的条数 写死的
            },
            categoryName:"",
            goodsData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            value: true,
            form: {
                goodsId: "",
                goodsIntroduce: "",
                goodsName: "",
                goodsNumber: "",
                goodsPrice: 0,
                hotGoods: "",
                goodsSmallLogo:""
            },
        };
    },
    computed:{
        data(){
          return this.goodsData.slice((this.query.pageIndex-1)*this.query.pageSize,(this.query.pageIndex*this.query.pageSize))
        }
    },
    created:function(){
        this.getData()
        
    },
    methods: {
        getData() {
           goodsAll().then(({data})=>{
              this.goodsData=data.all
              console.log(this.goodsData)
           }) 
        },
        //拿到批量删除的数据
        handleSelectionChange(val){
            this.delList=val 
        },
        //搜索
        handleSearch(){
          seachGoods(this.query.name).then(res=>{
             this.goodsData=res.data.data
          })
        },  
        editGoods(row){
            this.editVisible=true
            goodsById(row.goodsId).then(({data})=>{
                this.form.goodsId=data.data.goodsId
                this.form.goodsName=data.data.goodsName
                this.form.goodsNumber=data.data.goodsNumber
                this.form.goodsPrice=data.data.goodsPrice
                this.form.goodsIntroduce=data.data.goodsIntroduce
                this.value= data.data.hotGoods===0?true:false;
                this.form.goodsSmallLogo=data.data.goodsSmallLogo
            })
        },
        delGoods(row){
           let name=row.goodsName;
        this.$confirm('是否删除【'+name+"】", '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           delGoods(row.goodsId).then(res=>{
               if(res.code==200){
                   this.$message.success("操作成功");
                   this.getData()
               }
           })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
        //编辑框
        saveEdit(){
            let params=new FormData();
            params.append("goodsId",this.form.goodsId)
            params.append("goodsName",this.form.goodsName)
            params.append("goodsNumber",this.form.goodsNumber)
            params.append("goodsPrice",this.form.goodsPrice)
            params.append("goodsIntroduce",this.form.goodsIntroduce)
            params.append("hotGoods",this.value==true?0:1)
            params.append("goodsSmallLogo",this.form.goodsSmallLogo)
            updateGoods(params).then(res=>{
                if(res.code==200){
                    this.$message.success("编辑商品成功");
                    this.editVisible=false
                    this.getData()
                }
            })
        } ,
        //分页
        handlePageChange(current){
            this.query.pageIndex=current
            console.log(this.pageIndex)
        },
        //批量删除
        delAllSelection(){
            // console.log(val.goodsId)
            this.delList.forEach(element=>{
                delGoods(element.goodsId);
                this.getData();
            })
            this.$message.success("批量删除成功");
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
